package cashpointer.web.action;

import cashpointer.web.support.PropertyStore;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;

public class PrepareLastAction extends ActionSupport {
    private PropertyStore propertyStore;
    private String output;
    private static final Integer INDEX_OF_CHAR_WHICH_STARTS_VERSION = 4;
    private static final Integer LENGTH_OF_VERSION = 12;

    @Override
    public String execute() throws Exception {
        // Sciezka do katalogu z plikami XML.
        String path = propertyStore.getPropertyByName("web.path-to-xml-files");
        String urlPath = propertyStore.getPropertyByName("web.url-to-xml-files");

        // Katalog z plikami XML.
        File dir = new File(path);

        // Ostatnia wersja.
        String lastVersion = new String("00000000");

        // Przeszukanie katalogu w poszukiwaniu plikow z pozniejszymi wersjami.
        if (dir != null) {
            String[] files = dir.list();
            if (files != null) {
                for (String filename : files) {
                    if (filename != null) {
                        if (filename.startsWith("all.")) {
                            // Wyszukiwanie i zapisywanie pozniejszych wersji.
                            String filenameVersion = filename.substring(INDEX_OF_CHAR_WHICH_STARTS_VERSION, LENGTH_OF_VERSION);
                            if (lastVersion.compareTo(filenameVersion) < 0) {
                                lastVersion = filenameVersion;
                            }
                        }
                    }
                }
            }
        }

        // Tekst do wyswietlenia
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        sb.append("<cashpointers>\n");
        sb.append("<last>\n");

        // Czytam pliki all.[yyyyMMdd].[001-n]
        int fileIndex = 1;
        while (true) {
        	try {
        		String filePostfix = String.format("%3d", fileIndex).replaceAll(" ", "0");
		        File file = new File(path + "/all." + lastVersion + "." + filePostfix);
		        if (file != null && file.exists()) {
		            sb.append("<file>");
		            sb.append("<version>");
		            sb.append(lastVersion);
		            sb.append(".");
		            sb.append(filePostfix);
		            sb.append("</version>");
		            sb.append("<url>");
		            sb.append(urlPath);
		            sb.append("all.");
		            sb.append(lastVersion);
		            sb.append(".");
		            sb.append(filePostfix);
		            sb.append("</url>");
		            sb.append("</file>\n");
		            fileIndex++;
		        } else {
		        	break;
		        }
        	} catch (Exception e) {
        		// polykam wyjatek
        		break;
        	}
        }

        sb.append("</last>\n");
        sb.append("</cashpointers>");

        output = sb.toString();

        return SUCCESS;
    }

    /* Akcesory */
    public String getOutput() {
        return output;
    }

    public void setOutput(String output) {
        this.output = output;
    }

    public PropertyStore getPropertyStore() {
        return propertyStore;
    }

    public void setPropertyStore(PropertyStore propertyStore) {
        this.propertyStore = propertyStore;
    }
}
